import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;


public class MgWeapon implements ActionListener,MgCommon{
	private int damage;
	private int fireWait;
	private int reloadWait;
	private int maxCapacity;
	private int clip=0;
	private int consumptionRate;
	
	private int bulletSpeed=15;
	private int bulletSize;
	private Color bulletColor;
	private Timer reload;
	private Timer refire;
	
	public MgWeapon(int consumptionRate,int maxCapacity, int damage, int fireWait, int reloadWait, int bulletSize, Color bulletColor) {
		this.consumptionRate = consumptionRate;
		this.maxCapacity = maxCapacity;
		this.damage = damage;
		this.fireWait = fireWait;
		this.reloadWait = reloadWait;
		this.bulletSize = bulletSize;
		this.bulletColor = bulletColor;
		refire = new Timer(fireWait, this);
		refire.setActionCommand("refire");
		reload = new Timer(reloadWait, this);
		reload.setActionCommand("reload");
	}
	
	public int getDamage(){
		return damage;
	}
	public int getFireWait(){
		return fireWait;
	}
	public int getReloadWait(){
		return reloadWait;
	}
	public Timer getRefire(){
		return refire;
	}
	public Timer getReload(){
		return reload;
	}
	public boolean fire(){
		if(!refire.isRunning() && clip>=consumptionRate && !reload.isRunning()){
			refire.start();
			clip-=consumptionRate;
			return true;
		}else
			return false;
	}
	public int reload(int ammo){
		reload.start();
		int taken = this.clip-maxCapacity;
		if(Math.abs(taken)>ammo){
			taken = ammo;
			clip = Math.abs(ammo);
		}else
			clip = maxCapacity;
		return taken;
	}
	public int getMaxCapacity(){
		return maxCapacity;
	}
	public int getBulletSpeed(){
		return bulletSpeed;
	}
	public int getBulletSize(){
		return bulletSize;
	}
	public Color getBulletColor(){
		return bulletColor;
	}
	public int getClip(){
		return clip;
	}
	public int getConsumptionRate(){
		return consumptionRate;
	}
	public void setBulletSpeed(int bulletSpeed){
		this.bulletSpeed = bulletSpeed;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("reload"))
			reload.stop();
		if(e.getActionCommand().equals("refire"))
			refire.stop();
	}
	
}
